package com.xbongbong.workflow.service.field.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.parent.pojo.RangeTimePoJo;
import com.xbongbong.parent.util.RangeTimeUtil;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.pojo.FilterCondition;
import com.xbongbong.workflow.pojo.WorkflowRulePojo;
import com.xbongbong.workflow.service.field.AbstractWorkflowConditionFieldService;
import com.xbongbong.workflow.service.field.WorkflowConditionFieldService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/06/07 14:03
 */
@Service("workflowConditionUpdateTimeServiceImpl")
public class WorkflowConditionUpdateTimeServiceImpl extends AbstractWorkflowConditionFieldService implements WorkflowConditionFieldService {


    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.UPDATETIME.getType());
    }

    @Override
    public Object handlerThisFormField(PaasFormDataEntityExt paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, WorkflowNodeEntity workflowNodeEntity, FilterCondition filterCondition, WorkflowRulePojo workflowRulePojo) throws XbbException {
        return paasFormDataEntity.getUpdateTime();
    }

    @Override
    public Object handlerTargetFormField(Object object, PaasFormDataEntityExt paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField,
                                         WorkflowNodeEntity workflowNodeEntity, WorkflowRulePojo workflowRulePojo, FilterCondition filterCondition) throws XbbException {
        RangeTimePoJo rangeTimePoJo = RangeTimeUtil.getRangeTime(fieldAttrEntity, targetField, filterCondition.getSymbol(), paasFormDataEntity.getUpdateTime());
        if (Objects.isNull(rangeTimePoJo)) {
            return null;
        }
        filterCondition.setSymbol(rangeTimePoJo.getSymbol());
        filterCondition.setValue(rangeTimePoJo.getList());
        if (rangeTimePoJo.getList().size() == 1) {
            return rangeTimePoJo.getList().get(0);
        }
        return rangeTimePoJo.getList();
    }


}
